package com.ruoyi.pms.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.pms.domain.Customer;
import com.ruoyi.pms.domain.vo.CustomerVO;
import com.ruoyi.pms.mapper.CustomerMapper;
import com.ruoyi.pms.service.ICustomerService;
import com.ruoyi.system.domain.SysDictRegion;
import com.ruoyi.system.mapper.SysDictRegionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 客户Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-10
 */
@Service
public class CustomerServiceImpl implements ICustomerService 
{
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private SysDictRegionMapper sysDictRegionMapper;

    /**
     * 查询客户
     * 
     * @param id 客户主键
     * @return 客户
     */
    @Override
    public Customer selectCustomerById(String id)
    {
        return customerMapper.selectCustomerById(id);
    }

    /**
     * 查询客户列表
     * 
     * @param customer 客户
     * @return 客户
     */
    @Override
    public List<Customer> selectCustomerList(Customer customer)
    {
        return customerMapper.selectCustomerList(customer);
    }

    /**
     * 新增客户
     * 
     * @param customer 客户
     * @return 结果
     */
    @Override
    public int insertCustomer(Customer customer)
    {
        customer.setCreateTime(DateUtils.getNowDate());
        customer.setId(UUID.fastUUID().toString());
        return customerMapper.insertCustomer(customer);
    }

    /**
     * 修改客户
     * 
     * @param customer 客户
     * @return 结果
     */
    @Override
    public int updateCustomer(Customer customer)
    {
        customer.setUpdateTime(DateUtils.getNowDate());
        return customerMapper.updateCustomer(customer);
    }

    /**
     * 批量删除客户
     * 
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteCustomerByIds(String[] ids)
    {
        return customerMapper.deleteCustomerByIds(ids);
    }

    /**
     * 删除客户信息
     * 
     * @param id 客户主键
     * @return 结果
     */
    @Override
    public int deleteCustomerById(String id)
    {
        return customerMapper.deleteCustomerById(id);
    }

    @Override
    public Map<String, Object> selectCustomerVOList(Customer customer) {
        List<Customer> customerList = customerMapper.selectCustomerList(customer);
        List<String> codes1 = customerList.stream().map(Customer::getProvince).collect(Collectors.toList());
        List<String> codes2 = customerList.stream().map(Customer::getCity).collect(Collectors.toList());
        List<String> codes3 = customerList.stream().map(Customer::getCounty).collect(Collectors.toList());
        List<String> codes = new ArrayList<>();
        codes.addAll(codes1);
        codes.addAll(codes2);
        codes.addAll(codes3);
        Map<String, String> codeNameMap = new HashMap<>();
        if (codes.size() > 0) {
            codeNameMap = sysDictRegionMapper.selectList(new QueryWrapper<SysDictRegion>().in("code", codes))
                    .stream().collect(Collectors.toMap(SysDictRegion::getCode, SysDictRegion::getName));
        }
        List<CustomerVO> customerVOList = new ArrayList<>();
        for (Customer customerItem : customerList) {
            CustomerVO customerVO = new CustomerVO();
            BeanUtils.copyProperties(customerItem, customerVO);
            customerVO.setValue(customerItem.getCustomerName());
            customerVO.setProvinceName(codeNameMap.get(customerItem.getProvince()));
            customerVO.setCityName(codeNameMap.get(customerItem.getCity()));
            customerVO.setCountyName(codeNameMap.get(customerItem.getCounty()));
            customerVOList.add(customerVO);
        }
        Map map = new HashMap();
        map.put("pages", customerList);
        map.put("list", customerVOList);
        return map;
    }
}
